home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / cutNpaste / Motif+Xt / docut_xt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.0 KB  |  400 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18.  
  19. #include <X11/Intrinsic.h>
  20. #include <X11/StringDefs.h>
  21. #include <X11/Xatom.h>
  22. #include <X11/Xmu/Atoms.h>
  23. #include <X11/Xmu/StdSel.h>
  24.  
  25. extern char *getSelection();
  26. char *xferSelection;
  27. extern short xRot, yRot;
  28.  
  29. void LoseSelection();
  30.  
  31. void SelValue();
  32. void TarValue();
  33.  
  34. Boolean ConvertSelection();
  35.  
  36. /* This is defined in X11/Xmu/Atomms.h
  37.  * #define XA_CLIPBOARD(d)    XInternAtom((d),"CLIPBOARD",False)
  38.  */
  39.  
  40. /* This was for playing around with Motif XmRString
  41.  * Note  XmRString != XA_STRING
  42.  * #define XA_STRING2(d)    XInternAtom((d),"String",False)
  43.  */
  44.  
  45. #define MXCHARS 30
  46.  
  47. char *theSelection=NULL;    /* the selection - mol as text */
  48. char *augSelection=NULL;    /* the augmented selection - mol + rotation */
  49.  
  50.  
  51. /*
  52.  * pasteCB()
  53.  *        paste call back
  54.  *    
  55.  *        request the data as a string
  56.  */
  57. void pasteCB(Widget w) {
  58.  
  59.     Display *d = XtDisplay(w);
  60.     Time time_of_request = XtLastTimestampProcessed(d);
  61.  
  62. #ifdef EBUG
  63.     fprintf(stderr,"in Paste\n");
  64.  
  65.     /* Get the timestamp value, just to try it */
  66.     XtGetSelectionValue(w, XA_CLIPBOARD(d), 
  67.                            XA_TIMESTAMP(d), 
  68.                            TarValue, 
  69.                            NULL, 
  70.                            time_of_request); 
  71.  
  72.     /* Playing with compatibility */
  73.     XtGetSelectionValue(w, XA_CLIPBOARD(d), 
  74.                            XA_STRING2(d), 
  75.                            SelValue, 
  76.                            NULL, 
  77.                            time_of_request); 
  78.  
  79. #endif
  80.  
  81.     /* Get the targets values...  could check which
  82.      * targets are available and choose best suited for
  83.      * this app, but this app only takes "STRING"
  84.      */
  85.     XtGetSelectionValue(w, XA_CLIPBOARD(d), 
  86.                            XA_TARGETS(d), 
  87.                            TarValue, 
  88.                            NULL, 
  89.                            time_of_request); 
  90.  
  91.     /* Go ahead and get the String */
  92.     XtGetSelectionValue(w, XA_CLIPBOARD(d), 
  93.                            XA_STRING, 
  94.                            SelValue, 
  95.                            NULL, 
  96.                            time_of_request); 
  97.  
  98. }
  99.  
  100.  
  101. void copyCB(Widget w) {
  102.     int rv, incr;
  103.     char arot[MXCHARS];
  104.     Display *d = XtDisplay(w);
  105.     Time time_of_request = XtLastTimestampProcessed(d);
  106.  
  107.     if (theSelection)
  108.         free(theSelection);
  109.     if (augSelection)
  110.         free(augSelection);
  111.     theSelection = getSelection();
  112.     if (theSelection == NULL) {
  113. #ifdef EBUG
  114.         fprintf(stderr,"Nothing selected\n");
  115. #endif
  116.         XBell( d, 0);
  117.     } else {
  118.  
  119. #ifdef EBUG
  120.         fprintf(stderr,"theSelection %s\n",theSelection);
  121.         printf("\n%s\n", theSelection);
  122. #endif
  123.  
  124.         sprintf(arot,"_MOL\n%d\n%d\n",xRot,yRot);
  125.         incr = strlen(arot);
  126.         augSelection = (char *)malloc(sizeof(char)*strlen(theSelection)+incr+1);
  127.         sprintf(augSelection,"%s%s",arot,theSelection);
  128.         rv = XtOwnSelection(w, XA_CLIPBOARD(d), time_of_request, ConvertSelection, LoseSelection, NULL);
  129.  
  130.         if (rv == True) {
  131. #ifdef EBUG
  132.             fprintf(stderr,"own selection %d\n",XA_CLIPBBOARD(d));
  133. #endif
  134.             ;
  135.         } else {
  136.             XBell( d, 0);
  137. #ifdef EBUG
  138.             fprintf(stderr,"not own selection %d\n",XA_CLIPBOARD(d));
  139. #endif
  140.         }
  141.  
  142.  
  143.     }
  144. }
  145.  
  146. void LoseSelection(Widget w, Atom *selection)
  147. {
  148. #ifdef EBUG
  149.     fprintf(stderr,"Giving up selection\n");
  150. #endif
  151. }
  152.  
  153. Boolean ConvertSelection(Widget w,Atom *selection, Atom *target,
  154.                 Atom *type, caddr_t *value, unsigned long *length, int *format)
  155. {
  156.     Display* d = XtDisplay(w);
  157.     XSelectionRequestEvent* req =
  158.         XtGetSelectionRequest(w, *selection, (XtRequestId)NULL);
  159.  
  160. #ifdef EBUG
  161.     fprintf(stderr,"In ConvertSelection\n");
  162. #endif
  163.  
  164.     if (*target == XA_TARGETS(d)) {
  165.     Atom* targetP;
  166.     Atom* std_targets;
  167.     unsigned long std_length;
  168.     XmuConvertStandardSelection(w, req->time, selection, target, type,
  169.                   (caddr_t*)&std_targets, &std_length, format);
  170.     *value = XtMalloc(sizeof(Atom)*(std_length + 5));
  171.     targetP = *(Atom**)value;
  172.     *targetP++ = XA_STRING;
  173.     *targetP++ = XA_TEXT(d);
  174.     *targetP++ = XA_LENGTH(d);
  175.     *targetP++ = XA_LIST_LENGTH(d);
  176.     *targetP++ = XA_CHARACTER_POSITION(d);
  177.     *length = std_length + (targetP - (*(Atom **) value));
  178.     bcopy((char*)std_targets, (char*)targetP, sizeof(Atom)*std_length);
  179.     XtFree((char*)std_targets);
  180.     *type = XA_ATOM;
  181.     *format = 32;
  182.  
  183.     return True;
  184.     }
  185.  
  186.     if (*target == XA_LIST_LENGTH(d) ||
  187.     *target == XA_LENGTH(d))
  188.     {
  189.         long * temp;
  190.  
  191.         temp = (long *) XtMalloc(sizeof(long));
  192.         if (*target == XA_LIST_LENGTH(d))
  193.           *temp = 1L;
  194.         else            /* *target == XA_LENGTH(d) */
  195.           *temp = (long) strlen (augSelection);
  196.  
  197.         *value = (caddr_t) temp;
  198.         *type = XA_INTEGER;
  199.         *length = 1L;
  200.         *format = 32;
  201.         return True;
  202.     }
  203.  
  204.     if (*target == XA_CHARACTER_POSITION(d))
  205.     {
  206.         long * temp;
  207.  
  208.         temp = (long *) XtMalloc(2 * sizeof(long));
  209.         temp[0] = (long) 0;
  210.         temp[1] = strlen (augSelection);
  211.         *value = (caddr_t) temp;
  212.         *type = XA_SPAN(d);
  213.         *length = 2L;
  214.         *format = 32;
  215.         return True;
  216.     }
  217.  
  218.     if (*target == XA_STRING ||
  219.       *target == XA_TEXT(d) ||
  220.       *target == XA_COMPOUND_TEXT(d))
  221.     {
  222.         if (*target == XA_COMPOUND_TEXT(d))
  223.         *type = *target;
  224.         else
  225.         *type = XA_STRING;
  226.     if (*selection == XA_CLIPBOARD(d) ) {
  227.  
  228.         *length = strlen(augSelection);
  229.         *value =XtMalloc(sizeof(char)*(*length+1));
  230.         strcpy(*value,augSelection);
  231.  
  232.     } else {
  233.         *length = strlen (augSelection);
  234.         *value = augSelection;
  235.         fprintf(stderr,"value with 0 %s\n",value);
  236.         *value = augSelection;
  237.         fprintf(stderr,"value with 1 %s\n",value);
  238.     }
  239.         *format = 8;
  240.         return True;
  241.     }
  242.  
  243.     if (XmuConvertStandardSelection(w, req->time, selection, target, type,
  244.                     value, length, format))
  245.         return True;
  246.  
  247.     return False;
  248.  
  249. }
  250.  
  251. void
  252. SelValue(Widget w, caddr_t client_data, Atom *selection, Atom *type, 
  253.     caddr_t value, unsigned long *length, int *format)
  254. {
  255.     char *cptr;
  256.     char axRot[MXCHARS], ayRot[MXCHARS];
  257.     char head[MXCHARS];
  258.     int i,j,k;
  259.  
  260.     if (*type == 0 /*XT_CONVERT_FAIL */ || *length ==0) {
  261.         XBell( XtDisplay(w), 0);
  262.         return;
  263.     } else {
  264.  
  265.         /* got value, so interpret "_MOL" format */
  266.         cptr = value;
  267.         i = 0;
  268.         while ((*(cptr+i) != '\n')  && (i<MXCHARS)) {
  269.             head[i++] = *(cptr+i);
  270.         }
  271.         head[i] = '\0';
  272.         i++;
  273.         if ( strcmp(head,"_MOL") ) {
  274.             XBell( XtDisplay(w), 0);
  275.             fprintf(stderr,"Paste not in _MOL format\n");
  276.             return;
  277.         }
  278.  
  279.         j=0;
  280.         while ((*(cptr+i) != '\n') && (j<MXCHARS)) {
  281.             axRot[j++] = *(cptr+i);
  282.             i++;
  283.         }
  284.         axRot[j] = '\0';
  285.         i++;
  286.  
  287.         k = 0;    
  288.         while ((*(cptr+i) != '\n') && (k<MXCHARS)) {
  289.             ayRot[k++] = *(cptr+i);
  290.             i++;
  291.         }
  292.         ayRot[k] = '\0';
  293.         i++;
  294.  
  295.         xRot = (short) atoi(axRot);
  296.         yRot = (short) atoi(ayRot);
  297.                 
  298.             
  299.         *length = strlen(value+i);
  300.         xferSelection =(char *)XtMalloc(sizeof(char)*(*length+1));
  301.         strcpy(xferSelection, (char *) (value+i));
  302. #ifdef EBUG
  303.         fprintf(stderr,"xferSelection %s\n",xferSelection);
  304. #endif
  305.         XtFree(value);
  306.  
  307.     buildMolecule(xferSelection);
  308.  
  309.     drawScene();
  310.         return ;
  311.     }
  312. }
  313.  
  314.  
  315. void
  316. TarValue(Widget w, caddr_t client_data, Atom *selection, Atom *type, 
  317.     caddr_t value, unsigned long *length, int *format)
  318. {
  319.     int i,ii;
  320.     Atom* targetP;
  321.  
  322.     if (*type == 0 /*XT_CONVERT_FAIL */ || *length ==0) {
  323.         XBell( XtDisplay(w), 0);
  324.         return;
  325.     } else {
  326.         targetP = (Atom *)value;
  327.         fprintf(stderr,">>%d targets\n",*length);
  328.         for (i=0; i< *length; i++) {
  329.             ii = *targetP++;
  330.             fprintf(stderr,"target %d is %d\n",i,ii);
  331.         }
  332.         fprintf(stderr,">>end targets\n");
  333.     }
  334.     return;
  335. }
  336.  
  337.  
  338. void cutCB(Widget w) {
  339.     int rv, incr;
  340.     char arot[MXCHARS];
  341.     Display *d = XtDisplay(w);
  342.     Time time_of_request = XtLastTimestampProcessed(d);
  343.     char *empty;
  344.  
  345.     if (theSelection)
  346.         free(theSelection);
  347.     if (augSelection)
  348.         free(augSelection);
  349.     theSelection = getSelection();
  350.     if (theSelection == NULL) {
  351.         XBell( d, 0);
  352. #ifdef EBUG
  353.         fprintf(stderr,"Nothing selected\n");
  354. #endif
  355.     } else {
  356.  
  357. #ifdef EBUG
  358.         fprintf(stderr,"theSelection %s\n",theSelection);
  359.         printf("\n%s\n", theSelection);
  360. #endif
  361.  
  362.         sprintf(arot,"_MOL\n%d\n%d\n",xRot,yRot);
  363.         incr = strlen(arot);
  364.         augSelection = (char *)malloc(sizeof(char)*strlen(theSelection)+incr+1);
  365.         sprintf(augSelection,"%s%s",arot,theSelection);
  366.         rv = XtOwnSelection(w, XA_CLIPBOARD(d), time_of_request, ConvertSelection, LoseSelection, NULL);
  367.  
  368.         if (rv == True) {
  369. #ifdef EBUG
  370.             fprintf(stderr,"own selection\n");
  371. #endif
  372.             ;
  373.         } else {
  374.             XBell( d, 0);
  375. #ifdef EBUG
  376.             fprintf(stderr,"not own selection\n");
  377. #endif
  378.         }
  379.  
  380.  
  381.     }
  382.  
  383.  
  384.     /* now draw no molecules */
  385.     empty = (char *)malloc(sizeof(char));
  386.     *empty = '\0';
  387.     buildMolecule(empty);
  388.  
  389.     drawScene();
  390.         return ;
  391.  
  392. }
  393.  
  394. void quitCB(Widget w) {
  395.     Display *d = XtDisplay(w);
  396.  
  397.     XtCloseDisplay(d);
  398.     exit(0);
  399. }
  400.